﻿using Application.DTO;
using Application.IService;
using Infrastructure.Common;
using Infrastructure.IRepository;
using Microsoft.AspNetCore.Http;

namespace Application.Service
{
    public class BaseService<T>:IBaseService<T> where T : class,new()
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly IBaseRepository<T> baseRepository;
        public BaseService(IUnitOfWork unitOfWork, IBaseRepository<T> baseRepository)
        {
            this.unitOfWork = unitOfWork;
            this.baseRepository = baseRepository;
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file">图片文件</param>
        /// <returns></returns>
        public string UpLoad(IFormFile file)
        {
            return UpLoadHelpers.UpLoad(file);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体信息</param>
        public async Task<int> Insert(T entity)
        {
            await baseRepository.Insert(entity);
            return await unitOfWork.SaveChanges();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities">实体信息集合</param>
        public async Task<int> InsertBatch(List<T> entities)
        {
            await baseRepository.InsertBatch(entities); 
            return await unitOfWork.SaveChanges();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体信息</param>
        public async Task<int> Update(T entity)
        {
            await baseRepository.Update(entity);
            return await unitOfWork.SaveChanges();
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities">实体信息集合</param>
        public async Task<int> UpdateBatch(List<T> entities)
        {
            await baseRepository.UpdateBatch(entities);
            return await unitOfWork.SaveChanges();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体信息</param>
        public async Task<int> Delete(T entity)
        {
            await baseRepository.Delete(entity);
            return await unitOfWork.SaveChanges();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities">实体信息结合</param>
        public async Task<int> DeleteBatch(List<T> entities)
        {
            await baseRepository.DeleteBatch(entities); 
            return await unitOfWork.SaveChanges();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<T>> GetAll()
        {
            return await baseRepository.GetAll();
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageSize">每页显示数据条数</param>
        /// <param name="pageIndex">当前页页码</param>
        /// <returns></returns>
        public async Task<PageDTO<T>> GetAll(int pageSize, int pageIndex)
        {
            var list = await baseRepository.GetAll();
            PageDTO<T> dto = new PageDTO<T>(list.Skip((pageIndex-1)*pageSize).Take(pageSize), list.Count());
            return dto;
        }
    }
}